package com.lemon.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.AddEmployeeDTO;
import com.lemon.reggie.pojo.dto.EditAndAbleDTO;
import com.lemon.reggie.pojo.dto.LoginDTO;
import com.lemon.reggie.pojo.entity.Employee;
import com.lemon.reggie.service.EmployeeService;
import com.lemon.reggie.utils.BaseContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<?> login(@RequestBody @Valid LoginDTO loginDTO, HttpServletRequest request) {
        Employee employee = employeeService.login(loginDTO, request);
        return R.success(employee);
    }

    @PostMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        employeeService.logout(request);
        return R.success("登出成功！");
    }

    @GetMapping("/page")
    public R<?> pageEmployee(@RequestParam("page") Long currentPage,
                             @RequestParam("pageSize") Long pageSize,
                             @RequestParam(value = "name",required = false) String name) {
        IPage<Employee> page = employeeService.pageEmployee(currentPage, pageSize, name);
        return R.success(page);
    }

    @PostMapping
    public R<?> addEmployee(@RequestBody @Valid AddEmployeeDTO addEmployeeDTO) {
        log.info("------当前请求的线程id为:{}", Thread.currentThread().getId());
        employeeService.addEmployee(addEmployeeDTO);
        return R.success("添加成功！");
    }

    @PutMapping
    public R<?> editAndAble(@RequestBody @Valid EditAndAbleDTO editAndAbleDTO) {
        log.info("------当前请求的线程id为:{}", Thread.currentThread().getId());
        employeeService.enableAndDis(editAndAbleDTO);
        return R.success("修改成功！");
    }

    @GetMapping("/{id}")
    public R<?> getById(@PathVariable("id") String id) {
        id = "1556288107652526099";
        return R.success(employeeService.getById(id));
    }

}

